package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.Department;
import com.jumi.microservice.domain.dto.DepartmentDTO;
import com.jumi.microservice.domain.vo.DepartmentQuery;
import com.jumi.microservice.domain.vo.DepartmentRequest;
import com.jumi.microservice.domain.vo.DepartmentVO;
import com.jumi.microservice.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门管理controller组件
 *
 * @author liuz
 * @since 2020-07-18
 */
@RestController
@Api(tags = "部门管理相关接口")
@RequestMapping("/auth/department")
public class DepartmentController {

    /**
     * 部门管理模块的service组件
     */
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "新增部门数据", notes = "部门表新增一条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "部门名称", value = "departmentName"),
            @ApiImplicitParam(name = "父部门ID", value = "parentId"),
            @ApiImplicitParam(name = "排序", value = "orderNum"),
    })
    @PostMapping("/add")
    @WebLog(description = "新增部门数据")
    public ResponseResult add(@Validated @RequestBody DepartmentVO department, HttpServletRequest request) {
        DepartmentDTO departmentDto = vo2Dto(department);
        departmentService.insert(departmentDto, request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "更新部门数据", notes = "更新指定ID部门表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),
            @ApiImplicitParam(name = "部门名称", value = "departmentName"),
            @ApiImplicitParam(name = "排序", value = "orderNum"),
    })
    @PutMapping("/update")
    @WebLog(description = "更新部门数据")
    public ResponseResult update(@Validated @RequestBody DepartmentVO department, HttpServletRequest request) {
        DepartmentDTO dto = vo2Dto(department);
        departmentService.update(dto, request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "删除部门数据", notes = "删除指定ID部门表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @DeleteMapping("/delete")
    @WebLog(description = "删除部门数据")
    public ResponseResult delete(@RequestParam Long id, HttpServletRequest request) {
        departmentService.delete(id, request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "部门启用/停用", notes = "部门状态的启用/停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),
            @ApiImplicitParam(name = "status", value = "状态")
    })
    @PutMapping("/enableStatus")
    @WebLog(description = "部门状态变更")
    public ResponseResult enableStatus(@RequestParam Long id, Integer status, HttpServletRequest request) {
        departmentService.enableStatus(id, status, request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "单条数据查询", notes = "获取指定ID部门表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail")
    @WebLog(description = "删除部门数据")
    public ResponseResult<DepartmentVO> detail(@RequestParam Long id) {
        DepartmentDTO dep = departmentService.selectById(id);
        return ResultGenerator.genSuccessResult(dto2Vo(dep));
    }


    @ApiOperation(value = "查询分页", notes = "获取部门表分页列表数据")
    @GetMapping("/list")
    public ResponseResult<List<DepartmentVO>> list(DepartmentRequest request) {
        List<DepartmentVO> vos = departmentService.selectList(request);
        return ResultGenerator.genSuccessResult(vos);
    }


    @ApiOperation(value = "树形菜单", notes = "部门树形菜单展示")
    @GetMapping("/child")
    public ResponseResult<List<DepartmentQuery>> getChildDepartmentList() {
        List<DepartmentQuery> department = departmentService.selectSonDepartmentId();
        return ResultGenerator.genSuccessResult(department);
    }


    /**
     * 部门对象转换 dto->vo
     *
     * @param departmentDto 部门dto
     * @return 部门vo
     */
    private DepartmentVO dto2Vo(DepartmentDTO departmentDto) {
        DepartmentVO departmentVo = null;
        try {
            departmentVo = departmentDto.clone(DepartmentVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return departmentVo;
    }

    /**
     * 部门VO对象转换 vo->dto
     *
     * @param departmentVo 部门VO
     * @return 部门DTO
     */
    private DepartmentDTO vo2Dto(DepartmentVO departmentVo) {
        DepartmentDTO departmentDto = null;
        try {
            departmentDto = departmentVo.clone(DepartmentDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return departmentDto;
    }

    /**
     * 部门DO对象转换 do->vo
     *
     * @param departments 部门do集合
     * @return 部门VO集合
     */
    private List<DepartmentVO> pos2Vos(List<Department> departments) {
        List<DepartmentVO> pos = departments.stream().map(p -> {
            DepartmentVO vo = null;
            try {
                vo = p.clone(DepartmentVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }

}
